package wangjg.android.wifiadmin;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;

public class BizModule {
	
	public static void checkNetStatus(Context context, Intent intent) {
		SharedPreferences pref = context.getSharedPreferences(Constant.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    	String SSID = pref.getString("SSID","");
    	String openTime = pref.getString("openTime","");
    	String closeTime = pref.getString("closeTime","");
    	boolean forceClose = pref.getBoolean("forceClose", false);

    	if(SSID==null||SSID.length()==0){
    		return ;
    	}

    	if(!inTimeRange(openTime, closeTime)){ //关闭
    		if(forceClose){
    			MyWifiAdmin wifi = new MyWifiAdmin(context);
        		wifi.setWifiEnabled(false);
    		}
    	}else{ //开启
    		MyWifiAdmin wifi = new MyWifiAdmin(context);
    		if(!wifi.isWifiEnabled()){
    			wifi.setWifiEnabled(true);
    			Intent tIntent = new Intent(Constant.TASK_ACTION);
    			AlarmUtil.setAlarm(context, System.currentTimeMillis()+Constant.TASK_DELAY, tIntent);
    			return;
    		}
    		//链接
    		if(!wifi.isConnected() || !SSID.equals(wifi.getSSID())){
    			wifi.connectTo(SSID);
    		}
    	}
	}
	
	private static boolean inTimeRange(String startTime, String endTime){
		if(startTime==null||startTime.length()==0){
			return true;
		}
		if(endTime==null||endTime.length()==0){
			return true;
		}
		
		String startTime1 = startTime;
		String endTime1 = endTime;
		String startTime2 = null;
		String endTime2 = null;
		
		//20:00-06:00
		if(startTime.compareTo(endTime)>0){
			startTime1 = startTime;
			endTime1 = "23:59";
			startTime2 = "00:00";
			endTime2 = endTime;
		}
		
    	SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
    	String currentTime = dateFormat.format(new Date());
    	
		boolean isOpenTime = (currentTime.compareTo(startTime1) >= 0 && currentTime.compareTo(endTime1) <= 0);
		
		if(!isOpenTime && startTime2!=null && endTime2!=null){
			isOpenTime = (currentTime.compareTo(startTime2) >= 0 && currentTime.compareTo(endTime2) <= 0);
		}
		
		return isOpenTime;
	}
	
	public static void sendServiceCmd(Context context, Intent intent) {
		Intent cmd = new Intent(context, WifiService.class);
		cmd.putExtras(intent.getExtras());
		context.startService(cmd);
	}
	
	public static void setAlarm(Context context) {
		SharedPreferences pref = context.getSharedPreferences(Constant.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    	String alarm_interval = pref.getString("alarm_interval", ""+Constant.DEFAULT_INTERVAL);
    	Integer interval = 1000 * Integer.parseInt(alarm_interval);
		
		long triggerAtTime = System.currentTimeMillis();
		
		Intent intent = new Intent(Constant.ALARM_ACTION);
		intent.putExtra(Constant.SERVICE_COMMAND, Constant.CMD_CHECK_WIFI);
		AlarmUtil.setAlarm(context, triggerAtTime, interval, intent);
	}

	public static void cancelAlarm(Context context) {
		Intent intent = new Intent(Constant.ALARM_ACTION);
		intent.putExtra(Constant.SERVICE_COMMAND, Constant.CMD_CHECK_WIFI);
		AlarmUtil.cancelAlarm(context, intent);
		
		Intent intent2 = new Intent(Constant.TASK_ACTION);
		AlarmUtil.cancelAlarm(context, intent2);
	}
	
	public static void disableComponent(Context context, Class<?> clazz){
		PackageManager pm = context.getPackageManager();
		ComponentName compName = new ComponentName(context.getApplicationContext(), clazz);  
		pm.setComponentEnabledSetting(
			compName,
			PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
			PackageManager.DONT_KILL_APP
		);
	}
	
	public static void enableComponent(Context context, Class<?> clazz){
		PackageManager pm = context.getPackageManager();
		ComponentName compName = new ComponentName(context.getApplicationContext(), clazz);  
		pm.setComponentEnabledSetting(
			compName,
			PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
			PackageManager.DONT_KILL_APP
		);
	}
	
	
}
